<?php
namespace Common\Models;
use Common\BaseModel;
class Collects extends BaseModel
{
	public $res;
	public $redis_key="collects_user_";

    public function initialize()
    {
        parent::initialize();
        $this->setSource('f_collects');
    }


    /**
     * @func 查询收藏总数
     */
    public function getCollectsCount($user_id){
        $conditions = "user_id = ?1";
        $parameters = array(1 => $user_id);
        $count_array = $this :: count(array(
            $conditions,
            "column" => "id",
            "bind" => $parameters
        ));
        return $count_array;
    }




    /**
     * @func 根据收藏id查找商品
     * @return bool 当前收藏的商品
     */
    public function find_by_id($product_id,$site){
        $product_collect = $this->findFirst("product_id='{$product_id}' AND site='{$site}'");
        if(empty($product_collect)){
            return false;
        }else{
            return $product_collect->toarray();
        }
    }



    /**
     * @func 根据收藏id查找商品
     * @return bool 当前收藏的商品
     */
    public function find_by_ids($user_id,$product_id){
        $condition=['conditions'=>"id in($product_id) AND user_id=$user_id"];
        $res=$this->find($condition);
        return $res;
    }



    /**
     * @func 根据商品id查找收藏的商品
     * @param $product_id 产品id
     * @return bool 当前收藏的商品
     */
	public function find_by_productId($product_id){
        $product_collect = $this->findFirstByProductId($product_id);
        if(empty($product_collect)){
            return false;
        }else{
            return $product_collect->toarray();
        }
	}


    /**
     * 用户是否收藏了该商品
     */
    public function is_collect_user($user_id,$product_id,$site){
        $conditions="user_id={$user_id} AND product_id='{$product_id}' AND site='{$site}'";
        $result = $this->findFirst(array(
            $conditions,
        ));
        if($result){
            return true;
        }else{
            return false;
        }
    }


	public function  find_first($data){
        $product_collect = $this->findFirst('product_id="'.$data['product_id'].'" and site="'.$data['site'].'" and user_id='.$data['user_id']);
        if(empty($product_collect)){
            return false;
        }else{
            return $product_collect->toarray();
        }
    }

	/**
     * @func 收藏商品
     * @param $user_id 用户id
     * @param $params 商品id和站点
     */
	public function collect_save($params){
        $params['created_at'] = date('Y-m-d H:i:s');
        $params['updated_at'] = date('Y-m-d H:i:s');
        $result = $this::save($params);
        if($result){
            $this->del_cache($this->redis_key.$params['user_id']);
            return true;
        }else{
            return false;
        }
    }

    /**
     * @func 删除收藏
     *
     */
    public function del_collect($user_id,$product_id){
        $condition=['conditions'=>"id in($product_id) AND user_id=$user_id"];
        $res=$this->find($condition);
        $this->del_cache($this->redis_key.$user_id);
        return $res->delete();
//        $ret= $this->find('id='.$data['id']);
//        return $ret->delete();
    }

    /**
     * @param $user_id 用户id
     * @param $product_id 商品id
     */
    public function del_goodsSite_collect($goods_id,$site,$user_id){
        $condition=['conditions'=>"product_id='$goods_id' AND site='$site' AND user_id=$user_id"];
        $res=$this->find($condition);
        $this->del_cache($this->redis_key.$user_id);
        return $res->delete();
    }


    public function getList($data){
        $page = intval($data['page']);
        $size = intval($data['page_size']);
        if ($page > 0) {
            $page = ($page - 1) * $size;
        }
        $condetion='user_id='.$data['user_id'];
        $listObj = $this->find(array(
            $condetion,
            'order'=>'id desc',
            'limit'   => $size,
            'offset'  => $page
        ))->toarray();
        foreach($listObj as $key=>$value){
            $listObj[$key]['Site'] = $value['site'];
            $listObj[$key]['GoodsId'] = $value['product_id'];
        }
        return $listObj;
    }


	public function get_collect_array($user_id){
		$data=[
		'haodanku'=>[],
		'jd'=>[],
		'pdd'=>[],
        'vph'=>[]
		];
		 $redis_key=$this->redis_key.$user_id;
		$res=$this->get_cache($redis_key);
		if($res)return $res;
        $condetion='user_id='.$user_id;
		$res=$this->find($condetion)->toarray();
		if($res != []){
			foreach($res as $k =>$v){
			$data[$v['site']][]=$v['product_id'];
			}
		}
		$this->set_cache($redis_key,$data);
		        return $data;
		
    }

}
